package com.atzc.sink;



import com.atzc.bean.StartLogBean;
import com.atzc.utils.mysql.MysqlConnect;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class StartLogSinkMysql extends RichSinkFunction<StartLogBean> {

    private PreparedStatement ps = null;
    private Connection connection = MysqlConnect.getConnection();

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        try {
            String sql = "insert into startup_log_new( create_time,requestid,width,height,`schema`,os,osversion,appversion,imei,publiclongitude,publiclatitude,publiccitycode,devicename,publictoken,memno,meminfocache,create_date,import_date,update_time,mac,oaid,appname,appchannelid,androidid ) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
        }catch (SQLException e){
            System.out.println("error:启动日志编译sql出错了");
        }

    }

    @Override
    public void invoke(StartLogBean startLogBean, Context context) throws Exception {
        //设置字段
        try {
            ps.setString(1,startLogBean.getCreate_time());
            ps.setString(2,startLogBean.getRequestid());
            ps.setString(3,startLogBean.getWidth());
            ps.setString(4,startLogBean.getHeight());
            ps.setString(5,startLogBean.getSchema());
            ps.setString(6,startLogBean.getOs());
            ps.setString(7,startLogBean.getOsversion());
            ps.setString(8,startLogBean.getAppversion());
            ps.setString(9,startLogBean.getImei());
            ps.setString(10,startLogBean.getPubliclongitude());
            ps.setString(11,startLogBean.getPubliclatitude());
            ps.setString(12,startLogBean.getPubliccitycode());
            ps.setString(13,startLogBean.getDevicename());
            ps.setString(14,startLogBean.getPublictoken());
            ps.setString(15,startLogBean.getMemno());
            ps.setString(16,startLogBean.getMeminfocache());
            ps.setString(17,startLogBean.getCreate_date());
            ps.setString(18,startLogBean.getImport_date());
            ps.setString(19,startLogBean.getUpdate_time());
            ps.setString(20,startLogBean.getMac());
            ps.setString(21,startLogBean.getOaid());
            ps.setString(22,startLogBean.getAppname());
            ps.setString(23,startLogBean.getAppchannelid());
            ps.setString(24,startLogBean.getAndroidid());
        }catch (Exception e){
            System.out.println("error:启动日志插入数据出错了");
        }



        // 执行写入
        ps.execute();
    }


}
